package com.platform.usercenter.controller;

import com.platform.common.core.utils.poi.ExcelUtil;
import com.platform.common.core.web.controller.BaseController;
import com.platform.common.core.web.domain.AjaxResult;
import com.platform.common.core.web.page.TableDataInfo;
import com.platform.common.security.annotation.RequiresPermissions;
import com.platform.common.security.utils.SecurityUtils;
import com.platform.constants.SysUserConstants;
import com.platform.usercenter.domain.SysDeptPost;
import com.platform.usercenter.domain.SysOrganizationDept;
import com.platform.usercenter.service.ISysDeptPostService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 岗位管理
 *
 * @author platform
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/deptPost")
public class SysDeptPostController extends BaseController {
    @Resource
    private ISysDeptPostService sysDeptPostService;

    /**
     * 查询部门岗位列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysDeptPost sysDeptPost) {
        startPage();
        List<SysDeptPost> list = sysDeptPostService.selectSysDeptPostList(sysDeptPost);
        return getDataTable(list);
    }

    /**
     * 岗位管理-获取部门树
     */
    @GetMapping("/detpTree")
    public AjaxResult detpTree(SysOrganizationDept dept) {
        return AjaxResult.success(sysDeptPostService.detpTree(dept));
    }

    /**
     * 导出部门岗位列表
     */
    @RequiresPermissions("usercenter:deptPost:export")
    //@Log(title = "部门岗位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDeptPost sysDeptPost) {
        List<SysDeptPost> list = sysDeptPostService.selectSysDeptPostList(sysDeptPost);
        ExcelUtil<SysDeptPost> util = new ExcelUtil<SysDeptPost>(SysDeptPost.class);
        util.exportExcel(response, list, "部门岗位数据");
    }

    /**
     * 获取部门岗位详细信息
     */
    @RequiresPermissions("usercenter:deptPost:query")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable("postId") Long postId) {
        return success(sysDeptPostService.selectSysDeptPostByPostId(postId));
    }

    /**
     * 新增部门岗位
     */
    @RequiresPermissions("usercenter:deptPost:add")
    //@Log(title = "部门岗位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDeptPost sysDeptPost) {
        return toAjax(sysDeptPostService.insertSysDeptPost(sysDeptPost));
    }

    /**
     * 修改部门岗位
     */
    @RequiresPermissions("usercenter:deptPost:edit")
    //@Log(title = "部门岗位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDeptPost sysDeptPost) {
        return toAjax(sysDeptPostService.updateSysDeptPost(sysDeptPost));
    }

    /**
     * 删除部门岗位
     */
    @RequiresPermissions("usercenter:deptPost:remove")
    //@Log(title = "部门岗位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable Long[] postIds) {
        return toAjax(sysDeptPostService.deleteSysDeptPostByPostIds(postIds));
    }

    /**
     * 获取当前用户登录组织下的部门信息
     */
    @GetMapping("/selectUserOrgIdPost")
    public AjaxResult selectUserOrgIdPost() {
        Long orgId = SecurityUtils.getLoginUser().getOrgId();
        List<SysDeptPost> sysDeptPosts = new ArrayList<>();
        if (!Objects.isNull(orgId)) {
            SysDeptPost deptPostQuery = new SysDeptPost();
            deptPostQuery.setOrgId(orgId);
            deptPostQuery.setIsDelete(SysUserConstants.N);
            sysDeptPosts = sysDeptPostService.selectSysDeptPostList(deptPostQuery);
        }
        return AjaxResult.success(sysDeptPosts);
    }
}
